package com.wanpinghui.supplier.message.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.netease.nimlib.sdk.uinfo.model.NimUserInfo;
import com.wanpinghui.supplier.R;
import com.wanpinghui.supplier.message.nim.NimManager;
import com.wanpinghui.supplier.message.nim.WPHMsg;
import com.wanpinghui.supplier.utils.WTimeUtil;
import com.wanpinghui.supplier.widgets.MsgLeftAudioView;
import com.wanpinghui.supplier.widgets.MsgLeftDocView;
import com.wanpinghui.supplier.widgets.MsgLeftImgView;
import com.wanpinghui.supplier.widgets.MsgLeftOrderNotificationView;
import com.wanpinghui.supplier.widgets.MsgLeftOrderView;
import com.wanpinghui.supplier.widgets.MsgLeftTxtView;
import com.wanpinghui.supplier.widgets.MsgRightAudioView;
import com.wanpinghui.supplier.widgets.MsgRightDocView;
import com.wanpinghui.supplier.widgets.MsgRightImgView;
import com.wanpinghui.supplier.widgets.MsgRightOrderNotificationView;
import com.wanpinghui.supplier.widgets.MsgRightOrderView;
import com.wanpinghui.supplier.widgets.MsgRightTxtView;
import com.wanpinghui.wphlibrary.model.message.bean.MsgBean;
import com.wanpinghui.wphlibrary.model.oauth.bean.UserBean;
import com.wanpinghui.wphlibrary.model.order.bean.OrderItemBean;
import com.yunshl.yunshllibrary.recyclerview.BaseRecyclerViewAdapter;
import com.yunshl.yunshllibrary.utils.TextUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by xiongning on 17-12-4.
 */

public class P2PMsgAdapter extends BaseRecyclerViewAdapter {

    public static final int TYPE_MINE = 0;

    public static final int TYPE_OTHER = 1;

    private UserBean userBean;
    private OnMsgClickListener msgClickListener;

    public P2PMsgAdapter(Context context) {
        super(context);
    }

    public void setUserBean(UserBean userBean) {
        this.userBean = userBean;
    }

    public void setMsgClickListener(OnMsgClickListener msgClickListener) {
        this.msgClickListener = msgClickListener;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        if (viewType == TYPE_MINE) {

            View view = LayoutInflater.from(context).inflate(R.layout.item_p2p_message_right_new, parent, false);

            return new MsgRightHolder(view);

        } else {

            View view = LayoutInflater.from(context).inflate(R.layout.item_p2p_message_left_new, parent, false);

            return new MsgLeftHolder(view);

        }
    }

    public synchronized void addData(int position, MsgBean data) {
        if (datas == null) {
            datas = new ArrayList();
        }
        System.out.println("b============" + data.getId());
        boolean isFound = false;
        if (datas != null && datas.size() > 0) {
            for (MsgBean mb : (List<MsgBean>) datas) {
                if (mb.getId().equals(data.getId())) {
                    System.out.println("a============" + data.getId());
                    isFound = true;
                    mb.setSend_statu(data.getSend_statu());
                    mb.setMsg(data.getMsg());
                    notifyItemChanged(position);
                    break;
                }
            }
        }

        if (!isFound) {
            if (position == 0) {
                datas.add(position, data);
            } else {
                if (datas.size() >= position) {
                    datas.add(position, data);
                }
            }
            notifyItemInserted(position);
        }


    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder mViewHolder, int position) {
        super.onBindViewHolder(mViewHolder, position);

        if (getItemViewType(position) == TYPE_MINE) {
            MsgRightHolder holder = (MsgRightHolder) mViewHolder;
            final MsgBean mb = (MsgBean) datas.get(position);
            String account = MsgBean.getMyImId();
            NimUserInfo nui = NimManager.getInstance().getUser(account, null);
            if (nui != null && TextUtil.isNotEmpty(nui.getAvatar()))
                Glide.with(context).load(nui.getAvatar()).asBitmap().thumbnail(0.1f).centerCrop().placeholder(R.mipmap.message_icon_clientele).error(R.mipmap.message_icon_clientele)
                        .into(holder.iv_head);
            else
                holder.iv_head.setImageResource(R.mipmap.message_icon_clientele);
            holder.msg_img.setVisibility(View.GONE);
            holder.msg_txt.setVisibility(View.GONE);
            holder.msg_doc.setVisibility(View.GONE);
            holder.msg_audio.setVisibility(View.GONE);
            holder.msg_order.setVisibility(View.GONE);
            holder.msg_order_notification.setVisibility(View.GONE);
            switch (mb.getType()) {
                case WPHMsg.MSG_DOC:
                    holder.msg_doc.setVisibility(View.VISIBLE);
                    holder.msg_doc.initData(mb);
                    break;
                case WPHMsg.MSG_IMG:
                    holder.msg_img.setVisibility(View.VISIBLE);
                    holder.msg_img.initData(mb);
                    break;
                case WPHMsg.MSG_TXT:
                    holder.msg_txt.setVisibility(View.VISIBLE);
                    holder.msg_txt.initData(mb);
                    break;
                case WPHMsg.MSG_VOICE:
                    holder.msg_audio.setVisibility(View.VISIBLE);
                    holder.msg_audio.initData(mb);
                    break;
                case WPHMsg.MSG_ORDER:
                    holder.msg_order.setVisibility(View.VISIBLE);
                    holder.msg_order.initData(mb, Integer.parseInt(userBean.getUser().getId()), msgClickListener);
                    break;
                case WPHMsg.MSG_BID_SUCCESS:
                    holder.msg_order_notification.setVisibility(View.VISIBLE);
                    holder.msg_order_notification.initData(mb);
                    break;
                case WPHMsg.MSG_BID_RECHARGE:
                    holder.msg_order_notification.setVisibility(View.VISIBLE);
                    holder.msg_order_notification.initData(mb);
                    break;
                case WPHMsg.MSG_BID_FOLLOW:
                    holder.msg_order_notification.setVisibility(View.VISIBLE);
                    holder.msg_order_notification.initData(mb);
                    break;
            }
            long create_at=mb.getCreated_at();
            int len=(create_at+"").length();
            if(len<13)
                create_at=create_at*1000;
            String showTime = WTimeUtil.getChatTime(/*mb.getCreated_at()*/create_at);
            if (position < getItemCount() - 1) {
                MsgBean pre_mb = (MsgBean) datas.get(position + 1);
                if (WTimeUtil.isOneMinus(pre_mb.getCreated_at(), mb.getCreated_at())) {
                    holder.tv_show_time.setVisibility(View.GONE);
                } else {
                    holder.tv_show_time.setVisibility(View.VISIBLE);
                    holder.tv_show_time.setText(showTime);
                }
            } else {
                holder.tv_show_time.setVisibility(View.VISIBLE);
                holder.tv_show_time.setText(showTime);
            }
        } else {
            MsgLeftHolder holder = (MsgLeftHolder) mViewHolder;
            final MsgBean mb = (MsgBean) datas.get(position);
            String account = mb.getAccount();
            NimUserInfo nui = NimManager.getInstance().getUser(account, null);
            if (nui != null && TextUtil.isNotEmpty(nui.getAvatar()))
                Glide.with(context).load(nui.getAvatar()).asBitmap().thumbnail(0.1f).centerCrop().placeholder(R.mipmap.message_icon_clientele).error(R.mipmap.message_icon_clientele)
                        .into(holder.iv_head);
            else
                holder.iv_head.setImageResource(R.mipmap.message_icon_clientele);
            holder.msg_img.setVisibility(View.GONE);
            holder.msg_txt.setVisibility(View.GONE);
            holder.msg_doc.setVisibility(View.GONE);
            holder.msg_audio.setVisibility(View.GONE);
            holder.msg_order.setVisibility(View.GONE);
            holder.msg_order_notification.setVisibility(View.GONE);
            switch (mb.getType()) {
                case WPHMsg.MSG_DOC:
                    holder.msg_doc.setVisibility(View.VISIBLE);
                    holder.msg_doc.initData(mb);
                    break;
                case WPHMsg.MSG_IMG:
                    holder.msg_img.setVisibility(View.VISIBLE);
                    holder.msg_img.initData(mb);
                    break;
                case WPHMsg.MSG_TXT:
                    holder.msg_txt.setVisibility(View.VISIBLE);
                    holder.msg_txt.initData(mb);
                    break;
                case WPHMsg.MSG_VOICE:
                    holder.msg_audio.setVisibility(View.VISIBLE);
                    holder.msg_audio.initData(mb);
                    break;
                case WPHMsg.MSG_ORDER:
                    holder.msg_order.setVisibility(View.VISIBLE);
                    holder.msg_order.initData(mb, Integer.parseInt(userBean.getUser().getId()), msgClickListener);
                    break;
                case WPHMsg.MSG_BID_SUCCESS:
                    holder.msg_order_notification.setVisibility(View.VISIBLE);
                    holder.msg_order_notification.initData(mb);
                    break;
                case WPHMsg.MSG_BID_RECHARGE:
                    holder.msg_order_notification.setVisibility(View.VISIBLE);
                    holder.msg_order_notification.initData(mb);
                    break;
                case WPHMsg.MSG_BID_FOLLOW:
                    holder.msg_order_notification.setVisibility(View.VISIBLE);
                    holder.msg_order_notification.initData(mb);
                    break;
            }
            long create_at=mb.getCreated_at();
            int len=(create_at+"").length();
            if(len<13)
                create_at=create_at*1000;
            String showTime = WTimeUtil.getChatTime(/*mb.getCreated_at()*/create_at);
            if (position < getItemCount() - 1) {
                MsgBean pre_mb = (MsgBean) datas.get(position + 1);
                if (WTimeUtil.isOneMinus(pre_mb.getCreated_at(), mb.getCreated_at())) {
                    holder.tv_show_time.setVisibility(View.GONE);
                } else {
                    holder.tv_show_time.setVisibility(View.VISIBLE);
                    holder.tv_show_time.setText(showTime);
                }
            } else {
                holder.tv_show_time.setVisibility(View.VISIBLE);
                holder.tv_show_time.setText(showTime);
            }
        }
    }

   /* @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position, List payloads) {

        if(payloads.isEmpty()){
            onBindViewHolder(holder,position);
        }else {
            int headCount=0;
            int adjPositon=position-headCount;
            int count= getItemCount();
            if(adjPositon<count){
                onBindViewHolder(holder,adjPositon,payloads);
                return;
            }
        }
    }*/

    @Override
    public int getItemCount() {
        return datas == null ? 0 : datas.size();
    }

    @Override
    public int getItemViewType(int position) {

        MsgBean mb = (MsgBean) datas.get(position);

        if (mb.getDirect() == 0)
            return TYPE_MINE;
        else
            return TYPE_OTHER;
    }

    public MsgBean getData(int index) {
        if (getItemCount() > index) {
            return (MsgBean) datas.get(index);
        }
        return null;
    }

    class MsgLeftHolder extends RecyclerView.ViewHolder {

        ImageView iv_head;
        TextView tv_show_time;
        MsgLeftTxtView msg_txt;
        MsgLeftImgView msg_img;
        MsgLeftDocView msg_doc;
        MsgLeftAudioView msg_audio;
        MsgLeftOrderView msg_order;
        MsgLeftOrderNotificationView msg_order_notification;

        MsgLeftHolder(View itemView) {
            super(itemView);

            iv_head = (ImageView) itemView.findViewById(R.id.iv_head);
            tv_show_time = (TextView) itemView.findViewById(R.id.tv_show_time);
            msg_txt = (MsgLeftTxtView) itemView.findViewById(R.id.msg_txt);
            msg_img = (MsgLeftImgView) itemView.findViewById(R.id.msg_img);
            msg_doc = (MsgLeftDocView) itemView.findViewById(R.id.msg_doc);
            msg_audio = (MsgLeftAudioView) itemView.findViewById(R.id.msg_audio);
            msg_order = (MsgLeftOrderView) itemView.findViewById(R.id.msg_order);
            msg_order_notification = (MsgLeftOrderNotificationView) itemView.findViewById(R.id.msg_order_notification);
            //itemView.setLayoutParams(new GridLayoutManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        }
    }

    class MsgRightHolder extends RecyclerView.ViewHolder {

        ImageView iv_head;
        TextView tv_show_time;
        MsgRightTxtView msg_txt;
        MsgRightImgView msg_img;
        MsgRightDocView msg_doc;
        MsgRightAudioView msg_audio;
        MsgRightOrderView msg_order;
        MsgRightOrderNotificationView msg_order_notification;

        MsgRightHolder(View itemView) {
            super(itemView);

            iv_head = (ImageView) itemView.findViewById(R.id.iv_head);
            tv_show_time = (TextView) itemView.findViewById(R.id.tv_show_time);
            msg_txt = (MsgRightTxtView) itemView.findViewById(R.id.msg_txt);
            msg_img = (MsgRightImgView) itemView.findViewById(R.id.msg_img);
            msg_doc = (MsgRightDocView) itemView.findViewById(R.id.msg_doc);
            msg_audio = (MsgRightAudioView) itemView.findViewById(R.id.msg_audio);
            msg_order = (MsgRightOrderView) itemView.findViewById(R.id.msg_order);
            msg_order_notification = (MsgRightOrderNotificationView) itemView.findViewById(R.id.msg_order_notification);
            //itemView.setLayoutParams(new GridLayoutManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        }
    }

    public interface OnMsgClickListener {
        void onOrderClick(OrderItemBean orderItemBean);
    }
}
